package com.block.channel.service.monitor;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.block.channel.enums.MonitorChannelEnum;
import com.block.channel.model.dto.MonitorInfoDTO;
import com.block.channel.model.result.CallbackResult;
import com.block.channel.service.IChannelCallbackService;
import com.block.channel.service.IChannelMonitorService;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelBaseInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * http://www.test.com/notice?
 * imei_md5=__IMEI_MD5__
 * &oaid=___OAID__
 * &imei=___IMEI__
 * &ip=__IP__
 * &ip=__IP__
 * &ts=__TS__
 * &userid=__USER_ID__
 * &pid=__PLAN_ID__
 * &uid=__USER_ID__
 * &aid=__IDEA_ID__
 * &click_id=__CLICK_ID__
 * &ext_info={{EXT_INFO}}
 * &callType=v2
 * &actionType=CLICK
 */
@Service
public class BaiDuMonitorService implements IChannelMonitorService, IChannelCallbackService {

    private static Logger log = LogFactory.getCommLog();

    /**
     * 账户名字     账户ID                    AKEY
     * 臻享分期     58421927    NTg0MjE5Mjc=
     * 臻享分期1   58738541    NTg3Mzg1NDE=
     * 臻享分期2   58738542    NTg3Mzg1NDI=
     * 臻享分期3   58738543    NTg3Mzg1NDM=
     * 臻享分期4   58738544    NTg3Mzg1NDQ=
     * 臻享分期5   58738537    NTg3Mzg1Mzc=
     * 臻享分期6   58738538    NTg3Mzg1Mzg=
     * 臻享分期7   58738539    NTg3Mzg1Mzk=
     * 臻享分期8   58738540    NTg3Mzg1NDA=
     * 臻享分期9   58738545    NTg3Mzg1NDU=
     */
//    public static HashMap<String, String> AKEY_MAP = new HashMap<>();
//    static {
//
//        // zhenxiang   大搜
//        AKEY_MAP.put("58421927", "NTg0MjE5Mjc=");
//        AKEY_MAP.put("58738541", "NTg3Mzg1NDE=");
//        AKEY_MAP.put("58738540", "NTg3Mzg1NDA=");
//        AKEY_MAP.put("58738545", "NTg3Mzg1NDU=");
//
//        // 来钱吧
//        AKEY_MAP.put("60455243", "NjA0NTUyNDM=");
//        AKEY_MAP.put("60455244", "NjA0NTUyNDQ=");
//        AKEY_MAP.put("60455245", "NjA0NTUyNDU=");
//        AKEY_MAP.put("60455240", "NjA0NTUyNDA=");
//
//        // zhengxiang  信息路
//        AKEY_MAP.put("58738542", "NTg3Mzg1NDI=");
//        AKEY_MAP.put("58738543", "NTg3Mzg1NDM=");
//        AKEY_MAP.put("58738544", "NTg3Mzg1NDQ=");
//        AKEY_MAP.put("58738537", "NTg3Mzg1Mzc=");
//        AKEY_MAP.put("58738538", "NTg3Mzg1Mzg=");
//        AKEY_MAP.put("58738539", "NTg3Mzg1Mzk=");
//
//        // 来钱吧
//        AKEY_MAP.put("60455241", "NjA0NTUyNDE=");
//        AKEY_MAP.put("60455242", "NjA0NTUyNDI=");
//    }

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public MonitorInfoDTO handleRequestParam(ChannelBaseInfo channelBaseInfo, String actionType, Map<String, String[]> parameterMap, String paramContent) {

        actionType = "CLICK";
        MonitorInfoDTO monitorInfoDTO = new MonitorInfoDTO();

        monitorInfoDTO.setPlatform(MonitorChannelEnum.BAIDU.getValue());

        monitorInfoDTO.setChannelCode(channelBaseInfo.getChannelCode());
        monitorInfoDTO.setChannelName(channelBaseInfo.getChannelName());
        monitorInfoDTO.setMonitorInfo(JSON.toJSONString(parameterMap));

        String[] bd_vidArr = parameterMap.get("bd_vid");
        String[] oaidArr = parameterMap.get("oaid");
        String[] ipArr = parameterMap.get("ip");
        String[] tsArr = parameterMap.get("ts");
        String[] useridArr = parameterMap.get("userid");
        String[] pidArr = parameterMap.get("pid");
        String[] uidArr = parameterMap.get("uid");
        String[] aidArr = parameterMap.get("aid");
        String[] click_idArr = parameterMap.get("click_id");
        String[] ext_infoArr = parameterMap.get("ext_info");
        String[] sign = parameterMap.get("sign");
        String[] akeyArr = parameterMap.get("akey");

        // TODO: 2024/9/18
        if (ObjectUtil.isNotEmpty(bd_vidArr)){
            monitorInfoDTO.setUniqueKey(bd_vidArr[0]);
            monitorInfoDTO.setKeyType("bd_vid");
        }else  if (ObjectUtil.isNotEmpty(oaidArr)){
            monitorInfoDTO.setUniqueKey(oaidArr[0]);
            monitorInfoDTO.setKeyType("oaid");
        }else if (ObjectUtil.isNotEmpty(click_idArr)){
            monitorInfoDTO.setUniqueKey(click_idArr[0]);
            monitorInfoDTO.setKeyType("clickId");
        }else{
            monitorInfoDTO.setUniqueKey(RandomUtil.randomString(32));
            monitorInfoDTO.setKeyType("randomId");
        }

        if (ObjectUtil.isNotEmpty(ext_infoArr)){
            String extInfo = ext_infoArr[0];
            monitorInfoDTO.setCallbackInfo(extInfo);
        }

        if (ObjectUtil.isNotEmpty(useridArr)){
            String userid = useridArr[0];
            monitorInfoDTO.setExtInfo1(userid);
        }

        if (ObjectUtil.isNotEmpty(akeyArr)){
            String akey = akeyArr[0];
            monitorInfoDTO.setExtInfo(akey);
        }

        monitorInfoDTO.setActionType(actionType);

        return monitorInfoDTO;
    }

    @Override
    public CallbackResult handleCallback(ChannelBaseInfo channelBaseInfo, String function, Map<String, String[]> parameterMap, String paramContent) {

        if (StrUtil.equals("authcode", function)){
            /**
             * 处理授权码
             */
            String[] appId = parameterMap.get("appId");
            String[] signature = parameterMap.get("authCode");
            String[] state = parameterMap.get("state");
            String[] timestamp = parameterMap.get("timestamp");
            String[] userId = parameterMap.get("userId");

            // 请求accessToken, 根据authCode
//            handleVivoAuthCode(codeArr[0]);
        }

        return null;
    }


}
